<?php

//power by wshoto.com
if (!defined('IN_IA')) {
    die('Access Denied');
}
global $_W, $_GPC;

$openid = m('user')->getOpenid();
$member = m('member')->getMember($openid);

$status = intval($_GPC['status']) <= 3 ? intval($_GPC['status']) : '';

switch($status){
    case 0 :
        $member['statusText'] = '待付款';
        break;
    case 1 :
        $member['statusText'] = '已付款';
        break;
    case 2 :
        $member['statusText'] = '待收货';
        break;
    case 3 :
        $member['statusText'] = '已完成';
        break;
    default :
        $member['statusText'] = '其他';
        break;
}

$title = $member['statusText'] . ' 订单';

if ($_W['isajax']) {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;

    $commissionModel = p('commission');
    $orderStatistics = $commissionModel->getCommissionRecordStatistics($openid, array( 'o_status_'.$status ));

    $member['cg_money'] = 0;
    $member['commissioncount'] = 0;

    foreach ($orderStatistics as $statistic) {
        $member['cg_money'] += intval($statistic['cg_money_sum']);
        $member['commissioncount'] += $statistic['c_money_sum'];
    }

    $member['commissioncount'] = number_format($member['commissioncount'], 2);
    unset($orderStatistics);

    $commissionSet = $commissionModel->getSet();

    $member['commission'] = $commissionModel->getLevel($member['openid']);

    if(empty($member['commission'])){
        $member['commission']['levelname'] = $commissionSet['levelname'];
    }

    $orderLists = array();
    $orderids = array();
    $orderInfo = array();

    $orderLists = $commissionModel->getCommissionRecord($member['id'], $status, array(), ($pindex - 1) * $psize, $psize);

    if(!empty($orderLists)){
        foreach($orderLists as $orderList){
            if(!empty($orderList['orderid'])){
                $orderids[] = $orderList['orderid'];
                $orderInfo[$orderList['id']]['commission'] += $orderList['c_money'];
                $orderInfo[$orderList['id']]['level'] = $orderList['c_rank'];
            }
        }
    }

    // 去重
    $orderids = array_flip(array_flip($orderids));

    $lists = array();

    if (!empty($orderids)) {

        $listx = m('order')->getOrderInfo($orderids, array('id', 'ordersn', 'createtime', 'status'));

        # 以记录表为循环，防止在特殊分佣时
        # 出现一个订单多笔佣金而无法显示现象。
        if(!empty($listx)){

            $ids = array();

            foreach($orderLists as $orderList){

                foreach($listx as $item){

                    if($orderList['orderid'] == $item['id']){
                        $item['commission'] = number_format($orderInfo[$orderList['id']]['commission'], 2);
                        $item['createtime'] = date('Y-m-d H:i', $item['createtime']);

                        switch($item['status']){
                            case 0 :
                                $item['statusText'] = '待付款';
                                break;
                            case 1 :
                                $item['statusText'] = '已付款';
                                break;
                            case 2 :
                                $item['statusText'] = '待收货';
                                break;
                            case 3 :
                                $item['statusText'] = '已完成';
                                break;
                            default :
                                $item['statusText'] = '其他';
                                break;
                        }

                        switch($orderInfo[$orderList['id']]['level']){
                            case 1 :
                                $item['levelText'] = '一';
                                break;
                            case 2 :
                                $item['levelText'] = '二';
                                break;
                            case 3 :
                                $item['levelText'] = '三';
                                break;
                            default :
                                $item['levelText'] = '特殊';
                                break;
                        }

                        $lists[] = $item;
                    }
                }
            }

        }

        unset($orderInfo, $orderids, $orderStatistics);
    }

    if(DEVELOPMENT == 0){
        unset($orderLists);
        show_json(1, array('lists' => $lists, 'pagesize' => $psize, 'member' => $member, 'commissionset' => $commissionSet));
    }else{
        show_json(1, array('lists' => $lists, 'pagesize' => $psize, 'member' => $member, 'commissionset' => $commissionSet, 'debug'=> $orderLists));
    }

}

include $this->template('member/orderx');

/*

$openid = $_W['openid'];

$commissionModel = p('commission');

$base = $commissionModel->getAllNextMembers($openid, true);

$detail = $base->getAllSalesvolumes()->getAllWealths()->getDetail();
$member  = $base->member;

$salesAll = $detail->afterSalesAll;

$type = intval($_GPC['type']);

if(!empty($type)){
    $type = intval($_GPC['type']);
}

$member['commission'] = $commissionModel->getLevel($base->member['openid']);

if(empty($member['commission'])){
    $member['commission']['levelname'] = '环保使者';
}

$statusText = '';

switch($type){
    case 0 :
        $statusText = '已下单';
        break;
    case 1 :
        $statusText = '已付款';
        break;
    case 2 :
        $statusText = '已发货';
        break;
    case 3 :
        $statusText = '已完成';
        break;
    default :
        $statusText = '未知';
        break;
}

$sales = array();

$salesPrice = 0;
$commissionPrice = 0;

if(!empty($salesAll)){
    foreach($salesAll as $item){
        if($item['status'] == $type){
            $salesPrice += $item['goodsprice'];
            $commissionPrice += $item['commissionPrice'];
            $sales[] = $item;
        }
    }
}

$sales = arr_sort($sales, 'createtime', 'DESC');

// 网上找的，排序二维数组
function arr_sort($array, $key, $order = "asc", $RefreshKey = false){//asc是升序 desc是降序

    $arr_nums = $arr = array();

    foreach ($array as $k => $v) {
        $arr_nums[$k] = $v[$key];
    }

    if ($order == 'asc') {
        asort($arr_nums);
    } else {
        arsort($arr_nums);
    }

    foreach ($arr_nums as $k => $v) {
        if ($RefreshKey) {
            $arr[] = $array[$k];
        } else {
            $arr[$k] = $array[$k];
        }
    }

    return $arr;
}*/
